import datetime

from app.core.extensions import db


# 定义模型
class User(db.Model):
    __tablename__ = 'users'

    # 使用租户查询类
    from app.core.BaseQuery import TenantQuery
    query_class = TenantQuery

    user_id = db.Column(db.Integer, primary_key=True)
    nick_name = db.Column(db.String(50), default='微信用户')
    avatar_url = db.Column(db.String(255))
    gender = db.Column(db.String(10), default='未知')
    country = db.Column(db.String(50), default='')
    province = db.Column(db.String(50), default='')
    city = db.Column(db.String(50), default='')
    balance = db.Column(db.DECIMAL(10, 2), default=0.00)
    points = db.Column(db.Integer, default=0)
    mp_open_id = db.Column(db.String(50))
    gzh_open_id = db.Column(db.String(50))
    mobile = db.Column(db.String(20))
    unionid = db.Column(db.String(50))
    app_id = db.Column(db.String(50))
    create_time = db.Column(db.DateTime, default=datetime.datetime.now)
    usage_count = db.Column(db.Integer, default=0)  # 使用次数字段
    role = db.Column(db.String(20), default='user', comment='用户角色（如 admin, user 等）')
    distribution_rate = db.Column(db.Numeric(5, 2), default=0.00, comment='分销比例')
    tenant_id = db.Column(db.Integer, db.ForeignKey('tenants.id'), nullable=True, comment='租户ID')

    def to_dict(self):
        return {
            "user_id": self.user_id,
            "nickName": self.nick_name,
            "avatarUrl": self.avatar_url,
            "gender": self.gender,
            "country": self.country,
            "province": self.province,
            "city": self.city,
            "balance": str(self.balance),
            "points": self.points,
            "mp_open_id": self.mp_open_id,
            "gzh_open_id": self.gzh_open_id,
            "mobile": self.mobile,
            "unionid": self.unionid,
            "app_id": self.app_id,
            "usage_count": self.usage_count,  # 返回使用次数
            "role": self.role,  # 返回用户角色
            "distribution_rate": float(self.distribution_rate) if self.distribution_rate else 0.0,  # 返回分销比例
            "tenant_id": self.tenant_id,  # 返回租户ID
        }


class ShareRelation(db.Model):
    __tablename__ = 'share_relation'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    sharer_id = db.Column(db.Integer, nullable=False, comment='分享者用户ID')
    invitee_id = db.Column(db.Integer, nullable=False, unique=True, comment='被邀请者用户ID')
    created_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    tenant_id = db.Column(db.Integer, nullable=True, comment='租户ID')

    def to_dict(self):
        return {
            'id': self.id,
            'sharer_id': self.sharer_id,
            'invitee_id': self.invitee_id,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S'),
            'tenant_id': self.tenant_id
        }


class RewardRecord(db.Model):
    __tablename__ = 'reward_record'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, nullable=False, comment="奖励归属用户 ID")
    reward_type = db.Column(db.String(50), nullable=False, default='invite', comment="奖励类型")
    reward_stage = db.Column(db.Integer, nullable=False, comment="奖励阶段（如5/10/15）")
    reward_count = db.Column(db.Integer, nullable=False, comment="奖励数量")
    created_at = db.Column(db.DateTime, default=datetime.datetime.now, comment="创建时间")
    tenant_id = db.Column(db.Integer, nullable=True, comment='租户ID')

    def __repr__(self):
        return f"<RewardRecord user_id={self.user_id} reward_type={self.reward_type} count={self.reward_count}>"
